package com.lkm.app_ui.m.Imple.system_camera;

import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.Log;

import com.lkm.javatool.lang.RunnablePR;

import java.io.IOException;



 class SlientCamera implements SurfaceTexture.OnFrameAvailableListener {

    private static final String TAG = "slient_camera";
    private Camera mCamera;

    private SurfaceTexture surfaceTexture;

    public SlientCamera(Context context) {
        surfaceTexture = new SurfaceTexture(10);
        surfaceTexture.setOnFrameAvailableListener(this);
    }



    public boolean openCamere() {
        try {
            mCamera = Camera.open(0);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }



        Camera.Parameters params = mCamera.getParameters();
        params.setPreviewFormat(ImageFormat.NV21);







        params.setPreviewSize(320, 240);
        params.setPictureSize(1024, 768);
        mCamera.setParameters(params);

        if (mCamera == null) {



            Log.e(TAG, "openCamere, mCamera == null!");
            return false;
        }

        try {

            mCamera.setPreviewTexture(surfaceTexture);
            mCamera.startPreview();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
        return true;
    }

    public void closeCamera() {
        Log.i(TAG, "closeCamera");
        if (null != mCamera) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

    private volatile long isTackPictureing;

    private volatile RunnablePR<byte[], Void> bc;



    public boolean tackPicture(RunnablePR<byte[], Void> bc) {
        if (isTackPictureing != 0) {






            RunnablePR<byte[], Void> xbc = bc;
            if (xbc != null) {
                xbc.run(null);
            }
            return false;
        }
        this.bc = bc;
        isTackPictureing = System.currentTimeMillis();


















//        P.d("===== 开始对焦 " + System.currentTimeMillis());
//        mCamera.autoFocus(new Camera.AutoFocusCallback() {


//                P.d("===== 开始对焦 ok " + System.currentTimeMillis());




        mCamera.cancelAutoFocus();

        try {
            mCamera.takePicture(null, null, mPictureCallback);
        }catch (Exception e){

            RunnablePR<byte[], Void> xbc = bc;
            if (xbc != null) {
                xbc.run(null);
            }
            return false;
        }


        return true;
    }



    private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(final byte[] data, Camera camera) {
            isTackPictureing = 0;

            RunnablePR<byte[], Void> xbc = bc;
            bc = null;
            if (xbc != null) {
                xbc.run(data);
            }





            Log.i(TAG, "data time = " + System.currentTimeMillis() + "  curThread" + Thread.currentThread().getName());

        }
    };

    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        Log.i(TAG, "onFrameAvailable, surfaceTexture.getTimestamp() = " + surfaceTexture.getTimestamp());

    }


}